package com.system.goodsserver.service.impl;

import com.sun.org.apache.regexp.internal.RE;
import com.system.goodsserver.bean.*;
import com.system.goodsserver.mapper.*;
import com.system.goodsserver.pojo.*;
import com.system.goodsserver.service.GoodsService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Xuheng Zhou
 * @date 2020/7/7 10:43
 * @desc
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired(required = false)
    private GoodsMapper mapper;

    @Autowired(required = false)
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired(required = false)
    private GoodsBrandMapper goodsBrandMapper;
    @Autowired(required = false)
    private GoodsCoverMapper goodsCoverMapper;
    @Autowired(required = false)
    private GoodsDetailimgMapper goodsDetailimgMapper;
    @Autowired(required = false)
    private SpecificationsMapper specificationsMapper;
    @Autowired(required = false)
    private FlagMapper flagMapper;

    @Autowired(required = false)
    private GoodsFlagMapper goodsFlagMapper;
    @Autowired(required = false)
    private GoodsSpecificationsMapper goodsSpecificationsMapper;

    @Override
    public List<ResultGoodsSelectBean> selcetGoods(SelectGoodsParamBean selectGoodsParamBean) {
        Integer page = selectGoodsParamBean.getPage();
        Integer size = selectGoodsParamBean.getSize();
        page = (page - 1) * size;
        selectGoodsParamBean.setPage(page);

        System.out.println("------state------"+selectGoodsParamBean.getGoodsStatue());

        return mapper.selcetGoods(selectGoodsParamBean);
    }

    @Override
    public Integer selcetGoodsCount(SelectGoodsParamBean selectGoodsParamBean) {
        Integer page = selectGoodsParamBean.getPage();
        System.out.println("--" + page);
        //因为controller中先查询的所有数据，业务处理的时候，page已经减1改变了值，这里的page直接传入mapper进行查询
        Integer count = mapper.selcetGoodsCount(selectGoodsParamBean);

        return count;
    }

    @Override
    public List<GoodsType> selcetGoodsType() {
        return goodsTypeMapper.selectAllGoodsType();
    }

    @Override
    public List<GoodsBrand> selcetGoodsBrand() {
        return goodsBrandMapper.selcetAllGoodsBrand();
    }

    @Override
    public ResultGoodsDetailSelectBean selcetGoodsOneById(Integer goodsId) {
        ResultGoodsDetailSelectBean resultGoodsDetailSelectBean = new ResultGoodsDetailSelectBean();
        resultGoodsDetailSelectBean = mapper.selcetGoodsOneById(goodsId);
        if (resultGoodsDetailSelectBean != null) {
            //查询规格
            List<ResultSpecificationByGoodsId> specificationsList = specificationsMapper.selectSpecificationsByGoodsId(goodsId);
            resultGoodsDetailSelectBean.setSpecifications(specificationsList);
            //查询封面图片
            List<GoodsCover> coverList = goodsCoverMapper.selectGoodsImgByGoodsId(goodsId);
            resultGoodsDetailSelectBean.setGoodsCoverImg(coverList);
            //查询详情图片
            List<GoodsDetailimg> detailimgList = goodsDetailimgMapper.selectGoodsDetailImgByGoodsId(goodsId);
            resultGoodsDetailSelectBean.setGoodsDetailimg(detailimgList);
            return resultGoodsDetailSelectBean;
        }
        return null;
    }

    @Override
    public boolean updateSpecificationsIsPriceBySid(Integer specificationsId, Integer isCoupon, Integer isPrice, Double price, Date endtime) {
        return specificationsMapper.updateSpecificationsIsPriceBySid(specificationsId, isCoupon, isPrice, price, endtime) > 0 ? true : false;
    }

    @Override
    public List<ResultSpecificationByGoodsId> SelectSpecificationsByGid(Integer goodsId) {
        return specificationsMapper.selectSpecificationsByGoodsId(goodsId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addGoods(addGoodsBean addGoodsBean) {
        List<Specifications> specificationsList = addGoodsBean.getSpecificationsList();
        List<Integer> specificationsIdList = new ArrayList<>();

        List<String> flagList = addGoodsBean.getGoodsFlag();
        List<Integer> flagIdList = new ArrayList<>();

        //添加规格标志
        boolean tf = false;
        //添加关键字
        boolean tf1 = false;
        //添加商品
        boolean tf2 = false;
        //添加封面
        boolean tf3 = false;
        //添加详情
        boolean tf4 = false;
        //添加商品和标签的关系表
        boolean tf5 = false;
        //添加商品和规格的关系表
        boolean tf6 = false;


        if (specificationsList.size() > 0) {
            //说明前端传入了规格信息，增加规格信息
            for (Specifications s : specificationsList) {
                tf = specificationsMapper.insertSelective(s) > 0 ? true : false;
                if (tf == true) {
                    specificationsIdList.add(s.getSpecificationsId());
                }
            }
        }

        if (flagList.size() > 0) {
            //说明前端传入了商品关键字
            for (String flagName : flagList) {

                boolean existFlag = flagMapper.checkFlagByName(flagName) > 0 ? true : false;
                //说明输入的商品关键字不存在
                if (existFlag == false) {
                    Flag flag = new Flag();
                    flag.setFlagName(flagName);
                    tf1 = flagMapper.insertSelective(flag) > 0 ? true : false;
                    if (tf1 == true) {
                        flagIdList.add(flag.getFlagId());
                    }
                } else {
                    Integer existFlagId = flagMapper.selectFlagIdByName(flagName);
                    flagIdList.add(existFlagId);
                }

            }
        }
        Goods goods = new Goods();
        goods.setGoodsName(addGoodsBean.getGoodsName());
        goods.setGoodbrandId(addGoodsBean.getGoodBrandId());
        goods.setGoodstypeId(addGoodsBean.getGoodsTypeId());
        goods.setGoodsCreatetime(new Date());
        goods.setGoodsFreight(addGoodsBean.getGoodsFreight());
        goods.setGoodsIntroduce(addGoodsBean.getGoodsIntroduce());
        goods.setGoodsStatue(addGoodsBean.getGoodsStatue());
        goods.setGoodsIshot(addGoodsBean.getGoodsIsHot());
        goods.setGoodsIscoupon(addGoodsBean.getGoodsIscoupon());
        goods.setGoodsIsrush(addGoodsBean.getGoodsIsRush());
        goods.setGoodsWeight(addGoodsBean.getGoodsWeight());

        Integer goodsId = 0;
        tf2 = mapper.insertSelective(goods) > 0 ? true : false;

        if (tf2 == true) {
            goodsId = goods.getGoodsId();
        }
        List<String> coverList = addGoodsBean.getGoodsCoverImg();

        if (coverList.size() > 0 && coverList.size() <= 4) {
            for (String coverImg : coverList) {
                GoodsCover gc = new GoodsCover();
                gc.setGoodsId(goodsId);
                gc.setImg(coverImg);
                tf3 = goodsCoverMapper.insertSelective(gc) > 0 ? true : false;
                if (tf3 == true) {
                    System.out.println("添加封面图片成功！");
                }
            }

        }
        List<String> detailImgList = addGoodsBean.getGoodsDetailimg();

        if (detailImgList.size() > 0 && detailImgList.size() <= 4) {
            for (String detailImg : detailImgList) {
                GoodsDetailimg goodsDetailimg = new GoodsDetailimg();
                goodsDetailimg.setGoodsId(goodsId);
                goodsDetailimg.setImg(detailImg);
                tf4 = goodsDetailimgMapper.insertSelective(goodsDetailimg) > 0 ? true : false;
                if (tf4 == true) {
                    System.out.println("添加详情图成功！");
                }
            }
        }

        for (Integer flagId : flagIdList) {
            GoodsFlag goodsFlag = new GoodsFlag();
            goodsFlag.setGoodsId(goodsId);
            goodsFlag.setFlagId(flagId);

            tf5 = goodsFlagMapper.insertSelective(goodsFlag) > 0 ? true : false;
        }


        for (Integer sid : specificationsIdList) {
            GoodsSpecifications goodsSpecifications = new GoodsSpecifications();
            goodsSpecifications.setGoodsId(goodsId);
            goodsSpecifications.setSpecificationsId(sid);
            tf6 = goodsSpecificationsMapper.insertSelective(goodsSpecifications) > 0 ? true : false;
        }

        if (tf == true && tf2 == true && tf3 == true && tf4 == true && tf5 == true && tf6 == true) {
            return true;
        } else {
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoods(Integer goodsId) {
        //删除封面标志
        boolean flag = false;
        //删除详情图片标志
        boolean flag1 = false;
        //删除商品与规格的关系标志
        boolean tf1 = false;
        //删除商品规格
        boolean tf2 = false;
        //删除商品与关键字的关系
        boolean tf3 = false;
        //删除关键字
        boolean tf4 = false;

        //判断商品是否有封面图片
        List<GoodsCover> coverList = goodsCoverMapper.selectGoodsImgByGoodsId(goodsId);
        if (coverList.size() > 0) {
            //删除商品封面
            flag = goodsCoverMapper.deleteByGoodsId(goodsId) > 0 ? true : false;
        }
        //判断商品是否有详情图片
        List<GoodsDetailimg> detailimgList = goodsDetailimgMapper.selectGoodsDetailImgByGoodsId(goodsId);
        if (detailimgList.size() > 0) {
            //删除商品详情图片
            flag1 = goodsDetailimgMapper.deleteByGoodsId(goodsId) > 0 ? true : false;
        }


        //首先通过商品和规格关系表获取规格id，并删除关系表和相应规格
        List<Integer> SpecificationsIdlist = goodsSpecificationsMapper.selectSpecificationsIdByGoodsId(goodsId);
        //存在商品规格关系
        if (SpecificationsIdlist.size() > 0) {
            //删除关系表中的数据
            tf1 = goodsSpecificationsMapper.deleteGoodsSpecificationsByGoodsId(goodsId) > 0 ? true : false;
            //关系表数据删除成功，则删除规格信息
            if (tf1 == true) {

                for (Integer specificationsId : SpecificationsIdlist) {
                    tf2 = specificationsMapper.deleteByPrimaryKey(specificationsId) > 0 ? true : false;

                }

            }
        }
        //说明没有商品和规格没有关系数据
/*
        //获取标签关键字的flagid
        List<Integer> flagIdList = goodsFlagMapper.selectFlagIdByGoodsId(goodsId);
        //说明该商品有绑定的标签，
        if (flagIdList.size() > 0) {
            //删除表中的关系
            tf3 = goodsFlagMapper.deleteByGoodsId(goodsId) > 0 ? true : false;
            if (tf3 == true) {

                for (Integer flagid : flagIdList) {
                    tf4 = flagMapper.deleteByPrimaryKey(flagid) > 0 ? true : false;

                }

            }
        }*/
        //删除商品信息
        boolean tf = mapper.deleteByPrimaryKey(goodsId) > 0 ? true : false;
        if (tf == true || flag == true || flag1 == true || tf1 == true || tf2 == true) {
            return true;
        }
        return false;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoods(UpdateGoodsBean updateGoodsBean) {
        boolean tf1 = false;
        boolean tf = false;
        boolean tf2 = false;
        Integer goodsId = updateGoodsBean.getGoodsId();
        //编辑标签
        List<Flag> flagList = new ArrayList<>();
        List<String> goodsFlagStrs = updateGoodsBean.getGoodsFlagStrs();


        if(goodsFlagStrs!=null&&goodsFlagStrs.size()!=0){
            for (String goodsFlagStr : goodsFlagStrs) {
                Integer integer = flagMapper.selectByFlagNameAndGoodsId(goodsFlagStr,goodsId);
                if(integer!=null&&integer>0){
                    Integer integer1 = flagMapper.selectFlagIdByName(goodsFlagStr);
                    flagMapper.deleteGoodsFlagByFlagId(integer1,goodsId);
                    flagMapper.deleteByPrimaryKey(integer1);
                }
                //新增，则进行新增关键字，并且新增商品关系
                Flag flag1 = new Flag();
                flag1.setFlagName(goodsFlagStr);
                tf1 = flagMapper.insertSelective(flag1) > 0 ? true : false;
                //新增关键字成功并获取新增的id，再增加关系
                if (tf1 == true) {
                    //新增成功，再新增商品和标签关系
                    Integer flagid = flag1.getFlagId();
                    GoodsFlag goodsFlag = new GoodsFlag();
                    goodsFlag.setFlagId(flagid);
                    goodsFlag.setGoodsId(goodsId);
                    tf2 = goodsFlagMapper.insertSelective(goodsFlag) > 0 ? true : false;
                }
            }
        }

        flagList = updateGoodsBean.getGoodsFlag();
        //说明前端需要修改标签
        if (flagList != null && flagList.size() > 0) {
            for (Flag flag : flagList) {

                if (flag.getFlagId() != null) {
                    //说明存在，是修改，
                    tf = flagMapper.updateByPrimaryKeySelective(flag) > 0 ? true : false;
                } else {
                    //新增，则进行新增关键字，并且新增商品关系
                    Flag flag1 = new Flag();
                    flag1.setFlagName(flag.getFlagName());
                    tf1 = flagMapper.insertSelective(flag1) > 0 ? true : false;
                    //新增关键字成功并获取新增的id，再增加关系
                    if (tf1 == true) {
                        //新增成功，再新增商品和标签关系
                        Integer flagid = flag1.getFlagId();
                        GoodsFlag goodsFlag = new GoodsFlag();
                        goodsFlag.setFlagId(flagid);
                        goodsFlag.setGoodsId(goodsId);
                        tf2 = goodsFlagMapper.insertSelective(goodsFlag) > 0 ? true : false;
                    }

                }
            }
        }

        Goods goods = new Goods();
        goods.setGoodsId(updateGoodsBean.getGoodsId());
        goods.setGoodsName(updateGoodsBean.getGoodsName());
        goods.setGoodbrandId(updateGoodsBean.getGoodBrandId());
        goods.setGoodstypeId(updateGoodsBean.getGoodsTypeId());
        goods.setGoodsFreight(updateGoodsBean.getGoodsFreight());
        goods.setGoodsIntroduce(updateGoodsBean.getGoodsIntroduce());
        goods.setGoodsStatue(updateGoodsBean.getGoodsStatue());
        goods.setGoodsIshot(updateGoodsBean.getGoodsIsHot());
        goods.setGoodsIscoupon(updateGoodsBean.getGoodsIscoupon());
        goods.setGoodsIsrush(updateGoodsBean.getGoodsIsRush());
        goods.setGoodsWeight(updateGoodsBean.getGoodsWeight());

        boolean tf3 = mapper.updateByPrimaryKeySelective(goods) > 0 ? true : false;

        if (tf3 == true || tf == true || tf2 == true) {

            return true;
        }

        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsAddSpecificatios(AddSpecifications addSpecifications) {
        boolean tf1 = false;

        boolean tf = specificationsMapper.addSpecifications(addSpecifications) > 0 ? true : false;

        if (tf == true) {
            //新增规格成功
            //获取新增id
            Integer specificationId = addSpecifications.getSpecificationsId();
            //获取商品的id
            Integer goodsId = addSpecifications.getGoodsId();

            GoodsSpecifications goodsSpecifications = new GoodsSpecifications();
            goodsSpecifications.setGoodsId(goodsId);
            goodsSpecifications.setSpecificationsId(specificationId);

            //添加商品与库存的id
            tf1 = goodsSpecificationsMapper.insertSelective(goodsSpecifications) > 0 ? true : false;

        }
        if (tf1 == true) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsDeleteSpecificatios(Integer specificationsId) {
        boolean tf1 = false;
        //删除规格
        boolean tf = specificationsMapper.deleteByPrimaryKey(specificationsId) > 0 ? true : false;
        if (tf == true) {
            //规格删除成功
            //删除关系表的数据
            tf1 = goodsSpecificationsMapper.deleteGoodsSpecificationsBySpecificationsId(specificationsId) > 0 ? true : false;

        }
        if (tf1 == true) {
            return true;
        }
        return false;
    }

    @Override
    public boolean UpdateCover(GoodsCover goodsCover) {

        if (goodsCover != null && goodsCover.getGoodscoverId()!=null) {
            boolean tf = goodsCoverMapper.updateByPrimaryKeySelective(goodsCover) > 0 ? true : false;
            if (tf == true) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean UpdateDetailImg(GoodsDetailimg goodsDetailimg) {
        if (goodsDetailimg != null && goodsDetailimg.getDetailimgId()!=null) {
            boolean tf = goodsDetailimgMapper.updateByPrimaryKeySelective(goodsDetailimg) > 0 ? true : false;
            if (tf == true) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean updateGoodsSalesNumber(Integer goodsId, Integer salesGoodsNumber) {

        boolean tf=mapper.updateGoodsSalesNumber(goodsId,salesGoodsNumber)>0?true:false;
        if (tf==true){
            return true;
        }
        return false;
    }


}
